$(document).ready(function() {
	displayFaculty();
	displayProject();
	bindingProject();
});

$(window).bind('hashchange', function() {
	bindingProject();
});

function bindingProject(){
	var currentUrl = window.location.href;
	currentUrl = currentUrl.slice(currentUrl.indexOf('#') + 1);
	var category = currentUrl.slice(0, currentUrl.indexOf('#'));
	var id = currentUrl.slice(currentUrl.indexOf('#') + 1);
	getNews(getParams(category, id));
	getRandomProject(getRandomParams(category, id));
}

function getNews(params){
	sendRequest(params.url, 'GET', params.params, function(res){
		var data = JSON.parse(res);
		if(data){
			$('#newsTitle').html('');
			$('#newsTitle').append(data.Title).append("<br>");
			$('#newsTitle').append($('<small/>').append(drawSpan(SPAN_CALENDAR, '')).append(drawSpan('', data.Date)));
			$('#newsDesc').html(data.Desc);
			$('#newsSummary').html(data.Summary);
			$('#newsImage').attr('src', data.Image);
			$(newsUsername).html(data.Username);
		}
	}, function(err){
	});
}

function getRandomProject(params){
	sendRequest(params.url, 'GET', params.params, function(res){
		var data = JSON.parse(res);
		$('#newsRandom').append(drawRandomNew(data.slice(0, 3)));
		$('#newsRandom').append(drawRandomNew(data.slice(3, 6)));
	}, function(err){
	});
}

function getParams(category, id){
	var tempReturn;
	switch(category){
		case 'news': tempReturn = {
						url: URL_NEWS,
						params: {
							action: 'getById',
							idNews: id
						}
					}; break;
		case 'info': tempReturn = {
						url: URL_INFORMATION,
						params: {
							action: 'getById',
							idInformation: id
						}
					}; break;
		case 'activity': tempReturn = {
						url: URL_ACTIVITY,
						params: {
							action: 'getById',
							idActivity: id
						}
					}; break;
		default: break;
	}
	return tempReturn;
}

function getRandomParams(category, id){
	var tempReturn;
	switch(category){
		case 'news': tempReturn = {
						url: URL_NEWS,
						params: {
							action: 'getByLimit',
							fromNews: 0,
							toNews: 6
						}
					}; break;
		case 'info': tempReturn = {
						url: URL_INFORMATION,
						params: {
							action: 'getByLimit',
							fromInformation: 0,
							toInformation: 6
						}
					}; break;
		case 'activity': tempReturn = {
						url: URL_ACTIVITY,
						params: {
							action: 'getByLimit',
							fromActivity: 0,
							toActivity: 6
						}
					}; break;
		default: break;
	}
	return tempReturn;
}

function drawRandomNew(data){
	var ul = drawUl('list-unstyled');
	for(var index in data){
		ul.append(drawSmallNew(data[index]));
	}
	return drawDiv('col-md-6 col-lg-6 col-sm-6 col-xs-6').append(ul);
}